package org.tong.activiti6.test.commons;

import org.activiti.bpmn.model.FlowElement;
import org.activiti.bpmn.model.FlowNode;
import org.activiti.bpmn.model.SequenceFlow;
import org.activiti.engine.impl.interceptor.Command;
import org.activiti.engine.impl.interceptor.CommandContext;
import org.activiti.engine.impl.persistence.entity.ExecutionEntity;
import org.springframework.util.Assert;

import java.util.List;

public class JumpCommand implements Command<Void> {

    private FlowNode flowNode;
    private String executionId;

    public JumpCommand(FlowNode flowNode, String executionId) {
        this.flowNode = flowNode;
        this.executionId = executionId;
    }

    @Override
    public Void execute(CommandContext commandContext) {
        //来源节点连线
        List<SequenceFlow> incomingFlows = flowNode.getIncomingFlows();
        Assert.notNull(incomingFlows, "来源节点为空");
        Assert.notEmpty(incomingFlows, "来源连线不存在");
        ExecutionEntity executionEntity = commandContext.getExecutionEntityManager().findById(executionId);
        executionEntity.setCurrentFlowElement(incomingFlows.get(0));
        commandContext.getAgenda().planTakeOutgoingSequenceFlowsOperation(executionEntity, true);
        return null;
    }
}
